package com.herui.ota.service.iml;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.herui.ota.dao.mysql.OpsRecordMapper;
import com.herui.ota.entity.OpsRecord;
import com.herui.ota.enums.OpsTypeEnum;
import com.herui.ota.pojo.form.OpsRecordFilterForm;
import com.herui.ota.service.OpsRecordService;
import com.mlnx.common.form.PageForm;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * author name: amanda.shan
 * create time: 2024-04-28 22:55:45
 */
@Service
public class OpsRecordServiceIml extends BaseService<OpsRecordMapper, OpsRecord> implements OpsRecordService {

    @Autowired
    @Override
    public void setBaseMapper(OpsRecordMapper baseMapper) {
        super.setBaseMapper(baseMapper);
    }

    @Override
    public IPage page(PageForm pageForm, Object filter) throws IllegalAccessException {
        OpsRecordFilterForm opsRecordFilterForm = (OpsRecordFilterForm) filter;
        String types = opsRecordFilterForm.getTypes();
        opsRecordFilterForm.setTypes(null);
        QueryWrapper<OpsRecord> queryWrapper = this.getQueryWrapper(filter);
        if (!StringUtils.isEmpty(types)) {
            String[] split = types.split(",");
            queryWrapper.and( q ->{
                for (int i = 0; i < split.length; i++) {
                    int finalI = i;
                    q.or(query -> {

                        OpsTypeEnum opsTypeEnum = OpsTypeEnum.valueOf(split[finalI]);
                        query.eq("type", opsTypeEnum.getValue());

                    });
                }
            });
        }
        queryWrapper.orderByDesc("create_time");
        return this.page(new Page(pageForm.getCurrent(), pageForm.getSize()), queryWrapper);
    }

    @Override
    public void save(OpsTypeEnum type, Integer recordId, String msg) {
        OpsRecord opsRecord = new OpsRecord();
        opsRecord.setMsg(msg);
        opsRecord.setType(type);
        opsRecord.setRecordId(recordId);
        super.save(wrapperCreater(opsRecord));
    }

    @Override
    public List<Object> list(Object filter) throws IllegalAccessException {
        OpsRecordFilterForm opsRecordFilterForm = (OpsRecordFilterForm) filter;
        QueryWrapper<OpsRecord> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(opsRecordFilterForm.getTypes())) {
            String[] split = opsRecordFilterForm.getTypes().split(",");
            queryWrapper.and( q ->{
                for (int i = 0; i < split.length; i++) {
                    int finalI = i;
                    q.or(query -> {

                        OpsTypeEnum opsTypeEnum = OpsTypeEnum.valueOf(split[finalI]);
                        query.eq("type", opsTypeEnum.getValue());

                    });
                }
            });
        }
        if (opsRecordFilterForm.getRecordId() != null) {
            queryWrapper.eq("record_id", opsRecordFilterForm.getRecordId());
        }

        List<OpsRecord> list = list(queryWrapper);
        List<Object> collect = list.stream().map(opsRecord -> (Object) opsRecord).collect(Collectors.toList());
        return collect;
    }
}

